package danling

import (
	"encoding/base64"
	"encoding/json"
	"gitee.com/thingple/detonator/rest"
	"github.com/lishimeng/go-log"
)

type XpxxProjectInfo struct {
	LowVoltageId        string    `json:"lowVoltageId,omitempty"` // 流水号30Byte
	Conclusion          Qualified `json:"conclusion,omitempty"`
	TestTime            string    `json:"testTime,omitempty"`            // 精确到时分秒
	HighElectricity     string    `json:"highElectricity,omitempty"`     // 高压电流 13
	LowElectricity      string    `json:"lowElectricity,omitempty"`      // 低压电流 13
	Customer            string    `json:"customer,omitempty"`            // 雷管生产厂简称 13
	Resistance          Qualified `json:"resistance,omitempty"`          // 发火元件
	FireResistant       string    `json:"fireResistant,omitempty"`       // 发火元件阻值 13
	MosSet              Qualified `json:"mosSet,omitempty"`              // mos开关功能
	CapacitanceQuantity Qualified `json:"capacitanceQuantity,omitempty"` // 发火电容
	Type                string    `json:"type,omitempty"`                // 芯片类型
	BatchNo             string    `json:"batchNo,omitempty"`             //
}

func (c *client) RegisterDetonator(projects ...XpxxProjectInfo) (code int, err error) {

	req := Req{}
	req.Xpch = c.xpch

	for _, project := range projects {
		req.Projects = append(req.Projects, project)
	}

	var resp Resp

	bs, err := json.Marshal(req)
	if err != nil {
		return
	}
	//tmp := strings.ReplaceAll(string(bs), " ", "")
	log.Info("req:%s", bs)
	reqBs, err := encode(bs, c.desKey)
	if err != nil {
		return
	}
	reqContent := base64.StdEncoding.EncodeToString(reqBs)
	log.Info("req:%s", reqContent)
	params := make(map[string]string)
	params["param"] = reqContent
	code, err = rest.NewRest(c.host).Path("xpxxServlet", "addXpxx").ResponseJson(&resp).FormUrl(params)
	if err != nil {
		return
	}
	return
}
